package com.ssyc.student.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.view.MyListView;
import com.ssyc.student.R;
import com.ssyc.student.activity.StudentShowTextAnswerActivity;
import com.ssyc.student.activity.StudentTextPrepareActivity;
import com.ssyc.student.adapter.StAnswerLvAdapter;
import com.ssyc.student.bean.StAnswerInfo;
import com.ssyc.student.bean.TextPrepareInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--课文预习--fragment
 * Created by Administrator on 2018/5/10 0010.
 */

public class StudentTextChooseAnswerFragment extends LazyBaseFragment {

    //当前页面的数据模型
    private TextPrepareInfo.DataBean.QuestionListBean info;

    //阅读的题目内容
    private TextView tvTopic;

    //展示答案选项的ListView
    private MyListView lvAnswer;


    //展示正确的完整句子
    private TextView tvShowContent;

    //选择正确答案后需要展示的整体布局,默认不显示
    private LinearLayout llShowAnswer;


    //提供选择答案的数据集合
    private List<StAnswerInfo> answerInfos;

    //展示答案选项的列表适配器
    private StAnswerLvAdapter adapter;

    //当前题目的位置
    private int currPos;


    //通知Acitvity进行ViewPager翻页的标识
    public static String VPSCOLL = "StudentTextPrepareActivityVpScoll";


    //当前题目的正确答案,通过传值获取
    public String correctAnswer;

    //替换填空的下划线
    public static final String RESTR = "_____";

    //26个字母数组
    private String[] chartNums;


    //听力稿
    private String example;

    /**
     * 提供的生成当前Fragment的方法
     *
     * @param data 包含页面数据的json字符串
     * @return
     */
    public static StudentTextChooseAnswerFragment newInstance(TextPrepareInfo.DataBean.QuestionListBean data, int pos, String example) {

        StudentTextChooseAnswerFragment studentReadChooseAnswerFragment = new StudentTextChooseAnswerFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", data);
        bundle.putInt("pos", pos);
        bundle.putString("example", example);
        studentReadChooseAnswerFragment.setArguments(bundle);

        return studentReadChooseAnswerFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_text_choose_answer;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        showContent();

        //初始化视图
        initView(view);

        //模拟提供答案选项的数据
        initData();

        //设置展示答案的ListView
        initLv();
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {

        //获取A到Z的字母数组
        chartNums = getResources().getStringArray(R.array.base_letters);
        tvTopic = view.findViewById(R.id.tv_topic);
        lvAnswer = view.findViewById(R.id.lv_answer);
        tvShowContent = view.findViewById(R.id.tv_show_content);
        llShowAnswer = view.findViewById(R.id.ll_show_answer);
        llShowAnswer.setVisibility(View.GONE);
    }


    /**
     * 模拟的答案选项
     */
    private void initData() {
        //获取当前页面的位置
        currPos = getArguments().getInt("pos");

        //获取听力稿的内容
        example = getArguments().getString("example");
        //设置听力稿的内容
        tvShowContent.setText(example);

        //获取传递过来的页面数据
        info = (TextPrepareInfo.DataBean.QuestionListBean) getArguments().getSerializable("data");

        //获取当前题目的正确答案
        correctAnswer = info.getWordpic();

        //设置题目
        setTitleContent(info.getWordname());

        //配置选择答案的选项
        answerInfos = new ArrayList<>();


        List<TextPrepareInfo.DataBean.QuestionListBean.JpglistBean> jpglist = info.getJpglist();
        if (null != jpglist && jpglist.size() != 0) {
            for (int i = 0; i < jpglist.size(); i++) {
                StAnswerInfo info = new StAnswerInfo();
                info.setAnswerContent(jpglist.get(i).getText());
                info.setAnswerFlag(chartNums[i]);
                answerInfos.add(info);
            }
        }
    }

    /**
     * 设置题目的标题内容
     */
    private void setTitleContent(String titleContent) {
        if (!TextUtils.isEmpty(titleContent)) {
            tvTopic.setText((currPos + 1) + "." + titleContent.replace("填空", RESTR));
        }
    }

    /**
     * 设置展示选择答案的ListView
     */
    private void initLv() {
        adapter = new StAnswerLvAdapter(getContext(), answerInfos, R.layout.student_lv_answer);
        lvAnswer.setAdapter(adapter);
        lvAnswer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //条目的点击事件
                for (int i = 0; i < answerInfos.size(); i++) {
                    if (pos == i) {
                        answerInfos.get(pos).isSelect = true;
                    } else {
                        answerInfos.get(i).isSelect = false;
                    }
                }
                adapter.notifyDataSetChanged();

                //重新做题后需要通知提交界面的适配器数据刷新
                EventBus.getDefault().post(new BusInfo(StudentTextCommitFragment.HASDONE, currPos, answerInfos.get(pos).answerFlag));

                //将选择的答案保存到Acitivity的Map中
                StudentTextPrepareActivity.getChooseAnswerMap().put(currPos, answerInfos.get(pos).answerFlag);

                //判断选择答案的对错,然后保存到Map中,去除所有空格后进行比较,防止配置表出现多个空格导致错误
                if (answerInfos.get(pos).getAnswerContent().replaceAll(" ", "").equals(correctAnswer.replaceAll(" ", ""))) {
                    //选择正确
                    StudentTextPrepareActivity.getAnswerMap().put(currPos, true);
                } else {
                    //选择错误
                    StudentTextPrepareActivity.getAnswerMap().put(currPos, false);
                }

                //选择答案后跳转到下一页面,EventBus通知Activity进行ViewPager的翻页操作
                EventBus.getDefault().post(new BusInfo(VPSCOLL));
            }
        });
    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && StudentShowTextAnswerActivity.LOOKANSWER == busInfo.getType()) {

            //查看答案的标识
            for (int i = 0; i < answerInfos.size(); i++) {
                if (answerInfos.get(i).getAnswerContent().replaceAll(" ", "").equals(correctAnswer.replaceAll(" ", ""))) {
                    //当前位置是正确答案,则设置为选中状态
                    answerInfos.get(i).isSelect = true;
                } else {
                    //当前位置不是正确答案,设置为不选中状态
                    answerInfos.get(i).isSelect = false;
                }
            }

            adapter.notifyDataSetChanged();

            //同时设置列表不可在次进行点击操作
            lvAnswer.setEnabled(false);

            //让听力稿视图进行展示
            llShowAnswer.setVisibility(View.VISIBLE);
        }
    }
}
